<template>
  <div>
    <!-- 搜索列表 -->
    <van-cell @click="onsearch(item)"  v-for="item in list" :key="item">
      <template>
        <van-icon name="close"/>
      </template>
      <template #title>
        <div v-html="hightText(item)" ></div>
      </template>
    </van-cell>

  </div>
</template>

<script>
import { thinksuggestion } from '@/api/search'
export default {
  props: {
    search_text: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      list: []
    }
  },
  watch: {
    search_text: {
      immediate: true,
      handler (newval) {
        clearTimeout(this.timeid)
        this.timeid = setTimeout(async () => {
          const res = await thinksuggestion(newval)
          console.log(res)
          this.list = res.options
        }, 800)
      }
    }
  },
  created () {

  },

  methods: {
    hightText (item) {
      // 将需要替换的
      const reg = new RegExp(this.search_text, 'gi')
      return item.replace(reg, '<span style="color:red">' + this.search_text + '</span>')
    },
    onsearch (item) {
      console.log(item)
      this.$emit('onSearch', item)
    }
  },
  beforeDestroy () {
    clearTimeout(this.timeid)
  }

}
</script>

<style scoped lang='less'>

</style>
